<!-- 忘记密码 -->
<template>
  <div class="retrieve">
    <h3 class="title">{{VUE_APP_TITLE}}</h3>
    <el-form ref="retrieveForm" :model="retrieveForm" :rules="registerRules" class="register-form">

      <h2 class="mtip">重置密码</h2>
      <el-form-item prop="phonenumber">
        <el-input v-model="retrieveForm.phonenumber" type="text"  auto-complete="off" placeholder="手机号">
          <svg-icon slot="prefix" icon-class="phone" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>


      <el-form-item prop="smsCode" >
        <el-input v-model="retrieveForm.smsCode" type="text" auto-complete="off" placeholder="验证码">
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
          <el-button  type="text" slot="suffix" :disabled="getCodeBut" @click="getPhoneCode">
            <span v-if="getCodeBut">已发送 ({{countdownTime}})</span>
            <span v-else>获取验证码</span>
          </el-button>
        </el-input>
      </el-form-item>



      <el-form-item prop="password">
        <el-input v-model="retrieveForm.password" type="password" auto-complete="off" placeholder="密码" @keyup.enter.native="handleRegister">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="newPassword">
        <el-input v-model="retrieveForm.newPassword" type="password" auto-complete="off" placeholder="确认密码" @keyup.enter.native="handleRegister">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input v-model="retrieveForm.code" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter.native="handleRegister">
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="register-code">
          <img :src="codeUrl" @click="getCode" class="register-code-img" />
        </div>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="medium" type="primary" style="width:100%;" @click.native.prevent="handleRegister">
          <span v-if="!loading">提 交</span>
          <span v-else>提 交 中...</span>
        </el-button>
        <div style="float: right;">
          <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2024 指尖密码 All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg, forgotPassword,getCodeSms } from '@/api/login'

export default {
  name: 'retrieve',
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.retrieveForm.password !== value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      VUE_APP_TITLE: process.env.VUE_APP_TITLE,
      codeUrl: '',
      countdownTime:60,
      getCodeBut: false,

      retrieveForm: {
        username: '',
        phonenumber: '',
        password: '',
        newPassword: '',
        phonenumber: '',
        code: '',
        uuid: '',
   
      },
      registerRules: {
        phonenumber: [
          { required: true, trigger: 'blur', message: '请输入您的手机账号' },
          { pattern: this.$pattern.telRegex,   message: '请输入正确的手机账号',   trigger: ['blur']},
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' },
          {
            min: 5,
            max: 20,
            message: '用户密码长度必须介于 5 和 20 之间',
            trigger: 'blur',
          },
        ],
        newPassword: [
          { required: true, trigger: 'blur', message: '请再次输入您的密码' },
          { required: true, validator: equalToPassword, trigger: 'blur' },
        ],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }],
        smsCode: [{ required: true, trigger: 'change', message: '请输入验证码' }],
      },
      loading: false,
      captchaEnabled: false,
    }
  },
  created() {
    // this.getCode()
  },
  methods: {

    getPhoneCode() {
 
      // this.getCodeBut = true
      // this.countdownTime = 60
      // var timer = setInterval(() => {
      //   this.countdownTime--
      //   if (this.countdownTime == 0) {
      //     this.getCodeBut = false
      //     this.countdownTime = 60
      //     clearInterval(timer)
      //   }
      // }, 1000)

      this.$refs.retrieveForm.validateField('phonenumber', valid => {
        
        if (!valid) {
          getCodeSms({
            phonenumber : this.retrieveForm.phonenumber
          }).then(() => {
            this.getCodeBut = true
            this.countdownTime = 60
            var timer = setInterval(() => {
              this.countdownTime--
              if (this.countdownTime == 0) {
                this.getCodeBut = false
                this.countdownTime = 60
                clearInterval(timer)
              }
            }, 1000)
          })
        } 
      });


    },

    getCode() {
      getCodeImg().then((res) => {
        this.captchaEnabled =
          res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = 'data:image/gif;base64,' + res.data.img
          this.retrieveForm.uuid = res.data.uuid
        }
      })
    },
    handleRegister() {
      this.$refs.retrieveForm.validate((valid) => {
        if (valid) {
          this.loading = true
          let retrieveForm = this.retrieveForm

          forgotPassword(retrieveForm)
            .then((res) => {
              const username = this.retrieveForm.username
              this.$alert(
                '密码重置成功，请使用您的新密码登录系统',
                '系统提示',
                {
                  // dangerouslyUseHTMLString: true,
                  type: 'success',
                }
              )
                .then(() => {
                  this.$router.push('/login')
                })
                .catch(() => {})
            })
            .catch(() => {
              this.loading = false
              // if (this.captchaEnabled) {
              //   this.getCode()
              // }
            })
        }
      })
    },
  },
}
</script>

<style rel="stylesheet/scss" lang="scss"  scoped>
.retrieve {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url('../assets/images/login2.jpg');
  background-size: cover;

  .title {
    font-size: 4rem;
    margin-bottom: 3rem;
    text-align: center;
    color: #4C73D6;
    font-weight: 600;
  }
  .mtip {
    font-size: 2.4rem;
    margin: 0 auto 2rem auto;
    text-align: center;
    color: #333;
    font-weight: 600;
  }
  .register-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;
    .el-input {
      height: 38px;
      input {
        height: 38px;
      }
    }
    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }
  .register-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  .register-code {
    width: 33%;
    height: 38px;
    float: right;
    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }
  .el-register-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }
  .register-code-img {
    height: 38px;
  }
}
</style>
